import { defineStore } from "pinia";
import type { RouteLocationNormalizedLoaded } from "vue-router";
import router from "@/router";
import { getRawRoute } from "@/utils/routerHelper";
interface tagsViewFace {
  visitedViews: RouteLocationNormalizedLoaded[];
  cachedViews: Set<string>;
}

export const useTagsViewStore = defineStore("tags", {
  state: (): tagsViewFace => ({
    visitedViews: [],
    cachedViews: new Set(),
  }),
  getters: {
    getVisitedViews(): RouteLocationNormalizedLoaded[] {
      return this.visitedViews;
    },
    getCachedViews(): string[] {
      return Array.from(this.cachedViews);
    },
  },
  actions: {
    addView(tag: RouteLocationNormalizedLoaded) {
      this.addVisitedView(tag);
      this.addCachedView();
    },
    delView(tag: RouteLocationNormalizedLoaded) {
      this.delVisitedView(tag);
      this.addCachedView();
    },
    // 删除所有缓存和tag
    delAllViews() {
      this.delAllVisitedViews();
      this.addCachedView();
    },
    // 删除其他
    delOthersViews(tag: RouteLocationNormalizedLoaded) {
      this.delOthersVisitedViews(tag);
    },
    // 删除其他tag
    delOthersVisitedViews(view: RouteLocationNormalizedLoaded) {
      this.visitedViews = this.visitedViews.filter((v) => {
        console.log(v.path,view.path)
        return v?.meta?.affix || v.path === view.path;
      });
    },
    // 删除所有tag
    delAllVisitedViews() {
      const affixTags = this.visitedViews.filter((tag) => tag.meta.affix);
      this.visitedViews = affixTags;
    },

    // 新增tag
    addVisitedView(view: RouteLocationNormalizedLoaded) {
      if (this.visitedViews.some((v) => v.path === view.path)) return;
      if (view.meta?.noTagsView) return;
      this.visitedViews.push(
        Object.assign({}, view, {
          title: view.meta?.title || "no-name",
        })
      );
    },
    // 删除tag
    delVisitedView(view: RouteLocationNormalizedLoaded) {
      for (const [i, v] of this.visitedViews.entries()) {
        if (v.path === view.path) {
          this.visitedViews.splice(i, 1);
          break;
        }
      }
    },
    // 新增缓存
    addCachedView() {
      // console.log(view);
      // const route = getRawRoute(view);
      // const name = String(route);
      // if (!name) return;
      // if (this.cachedViews.has(name)) {
      //   return;
      // }
      // this.cachedViews.add(name);
      // 简单点，每次tag变化，重新取tag内的数组进行 添加缓存
      const cacheMap: Set<string> = new Set();
      for (const v of this.visitedViews) {
        const item = getRawRoute(v);
        const needCache = !item.meta?.noCache;
        if (!needCache) {
          continue;
        }
        const name = item.name as string;
        cacheMap.add(name);
      }
      if (
        Array.from(this.cachedViews).sort().toString() ===
        Array.from(cacheMap).sort().toString()
      )
        return;
      this.cachedViews = cacheMap;
    },
    // 删除缓存
    delCachedView() {
      const route = router.currentRoute.value;
      const index = this.getCachedViews.findIndex((v) => v === route.name);
      if (index > -1) {
        this.cachedViews.delete(this.getCachedViews[index]);
      }
    },
  },
});
